package com.ruicar.afs.cloud.bizcommon.print.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormClass;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormClassService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.bizcommon.vo.TemplateTreeVo;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author phong
 * @version 1.0
 * @description: 打印模板管理模块
 * @created 2020/5/19 11:38
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(value = "templateClass", description = "模板类别管理")
@RequestMapping("/templateClass")
public class ComPrintFormClassController {

    private ComPrintFormClassService comPrintFormClassService;
    private ComPrintFormManageService comPrintFormManageService;

    @Transactional
    @PostMapping("/add")
    @ApiOperation(value = "新增模板类别")
    public IResponse<Boolean> add(@RequestBody @Validated ComPrintFormClass comPrintFormClass) throws ParseException {
        return IResponse.success(comPrintFormClassService.saveTemplateClass(comPrintFormClass));
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑模板类别")
    public IResponse<Boolean> modifyTemplate(@RequestBody @Validated ComPrintFormClass comPrintFormClass) {
        return IResponse.success(comPrintFormClassService.modifyTemplateClass(comPrintFormClass));
    }

    @DeleteMapping("/del/{classNo}")
    @ApiOperation(value = "删除模板类别")
    public IResponse<Boolean> delTemplate(@PathVariable String classNo) {
        return IResponse.success(comPrintFormClassService.delTemplateClass(classNo));
    }

    @PostMapping("/tree")
    @ApiOperation(value = "模板类别树")
    @SysLog(value = "模板类别树")
    public IResponse<List<TemplateTreeVo>> tree() {

        // 查询所有类别
        List<ComPrintFormClass> list = comPrintFormClassService.list(Wrappers.<ComPrintFormClass>lambdaQuery()
                .orderByAsc(ComPrintFormClass::getClassId));
        // 查询所有模板
        List<ComPrintFormManage> templates = comPrintFormManageService.list(Wrappers.<ComPrintFormManage>lambdaQuery()
                .orderByDesc(ComPrintFormManage::getPrintFormId));

        Map<String, List<TemplateTreeVo>> maps = CollUtil.newHashMap();

        templates.forEach(item -> {
            TemplateTreeVo o = new TemplateTreeVo();
            o.setClassNo(item.getClassNo());
            o.setTemplateId(item.getId().toString());
            o.setTitle(item.getPrintFormName());
            List<TemplateTreeVo> vos = null;
            if (maps.containsKey(item.getClassNo())) {
                vos = maps.get(item.getClassNo());
            } else {
                vos = CollUtil.newArrayList();
            }
            vos.add(o);
            maps.put(item.getClassNo(), vos);
        });

        List<TemplateTreeVo> vos = list.stream().map(o -> {
            TemplateTreeVo vo = new TemplateTreeVo();
            vo.setClassId(NumberUtil.toStr(o.getId(), StrUtil.EMPTY));
            vo.setTitle(o.getClassName());
            vo.setClassNo(o.getClassId());
            vo.setTemplateId(StrUtil.EMPTY);
            vo.setChildren(maps.get(o.getClassId()));
            return vo;
        }).collect(Collectors.toList());
        // 查询所有模板

        return new IResponse<List<TemplateTreeVo>>().setData(vos);
    }

}
